package com.jinxin.platform.cdysylives.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdysylives.bean.CdysylivesDeviceTask;
import com.jinxin.platform.cdysylives.bean.CdysylivesMonitorDevice;
import com.jinxin.platform.cdysylives.bean.CdysylivesSecretkey;
import com.jinxin.platform.cdysylives.bean.bo.*;
import com.jinxin.platform.cdysylives.common.CommonCodeEntity;
import com.jinxin.platform.cdysylives.common.Constant;
import com.jinxin.platform.cdysylives.common.ResultEntity;
import com.jinxin.platform.cdysylives.mapper.CdysylivesDeviceTaskMapper;
import com.jinxin.platform.cdysylives.mapper.CdysylivesMonitorDeviceMapper;
import com.jinxin.platform.cdysylives.mapper.CdysylivesSecretkeyMapper;
import com.jinxin.platform.cdysylives.redis.RedisUtil;
import com.jinxin.platform.cdysylives.rpc.CdysyliveRpc;
import com.jinxin.platform.cdysylives.service.IEquipmentService;
import com.jinxin.platform.cdysylives.utils.HttpClientPoolUtil;
import com.jinxin.platform.cdysylives.utils.HttpClientResult;
import com.jinxin.platform.cdysylives.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Slf4j
public class IEquipmentServiceImpl implements IEquipmentService {
    @Autowired
    private RedisUtil redisUtil;
    @Value("${cdysylives.api-url.get-token}")
    private String tokenUrl;
    @Value("${cdysylives.api-url.get-address}")
    private String addressUrl;
    @Value("${cdysylives.api-url.list-video}")
    private String videoUrl;
    @Value("${cdysylives.api-url.list-device}")
    private String deviceUrl;
    @Value("${cdysylives.api-url.list-camera}")
    private String cameraUrl;
    @Value("${cdysylives.api-url.list-channel}")
    private String channelUrl;
    @Value("${cdysylives.api-url.list-file}")
    private String fileListUrl;
    @Value("${cdysylives.api-url.del-file}")
    private String delFileUrl;
    @Autowired
    private CdysylivesMonitorDeviceMapper monitorDeviceMapper;
    @Autowired
    private CdysylivesSecretkeyMapper secretkeyMapper;
    @Autowired
    private CdysyliveRpc cdysyliveRpc;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CdysylivesDeviceTaskMapper deviceTaskMapper;

    public ResultEntity getToken(){
//        String tokenKey = Constant.YSY_ACCESS_TOKEN;
//        if(redisUtil.hasKey(tokenKey)){
//            return new ResultEntity(CommonCodeEntity.SUC_CODE,"获取token成功",redisUtil.get(tokenKey));
//        }
        String jsonStr = (String)redisUtil.get(Constant.YSY_APPKEY_SECRET);
        if(StringUtils.isEmpty(jsonStr)){
            return new ResultEntity(CommonCodeEntity.FAIL,"未配置应用秘钥信息，获取token失败",null);
        }
        AppKeySecretInfo keySecretInfo = JSONObject.parseObject(jsonStr, AppKeySecretInfo.class);
        if(keySecretInfo == null){
            return new ResultEntity(CommonCodeEntity.FAIL,"获取应用秘钥信息失败",null);
        }
        return queryToken(keySecretInfo.getAppKey(),keySecretInfo.getSecret());
    }

    @Override
    public ResultEntity queryToken(String appKey,String secret){
        Map<String,String> params = new HashMap<>();
        params.put("appKey",appKey);
        params.put("appSecret",secret);
        try {
            HttpClientResult clientResult = HttpClientPoolUtil.doPost(tokenUrl, params, true);
            if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult.getCode())){
                DataResult response = JSON.parseObject(clientResult.getContent(), DataResult.class);
                JSONObject jsonObject = response.getData();
                if(jsonObject != null){
                    String accessToken = jsonObject.getString("accessToken");
                    Long expireTime = jsonObject.getLong("expireTime");// 具体过期时间，精确到毫秒
                    long time = expireTime > 0 ? (expireTime - System.currentTimeMillis())/1000 : 0;
                    ///redisUtil.set(tokenKey,accessToken,time);//单位是秒
                    return new ResultEntity(CommonCodeEntity.SUC_CODE,"获取token成功",accessToken);
                }

            }
            return new ResultEntity(CommonCodeEntity.FAIL,"请求失败",null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultEntity(CommonCodeEntity.FAIL,"获取token失败",null);
    }

    @Override
    public Map<String, Object> listDevices() {
        Map<String,Object> resultMap = new HashMap<>();
        ResultEntity tokenEntity = getToken();
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            resultMap.put("code",CommonCodeEntity.FAIL);
            resultMap.put("msg",tokenEntity.getResultDesc());
            return resultMap;
        }
        Map<String, String> params =new HashMap<>();
        params.put("accessToken", (String) tokenEntity.getResultContent());
        // 个人版设备接入上限：10台，无分页功能；如需更完善的功能,请升级为企业版
        params.put("pageStart", "0");
        params.put("pageSize", "50"); //默认为10，最大为50
        int count = 0;
        List<DeviceInfo> data = new ArrayList<>();
        try {
            HttpClientResult clientResult = HttpClientPoolUtil.doPost(deviceUrl, params, true);
            if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult.getCode())){
                DataResult2 response = JSON.parseObject(clientResult.getContent(), DataResult2.class);
                if(response == null || !"200".equals(response.getCode())){
                    resultMap.put("code",CommonCodeEntity.FAIL);
                    resultMap.put("msg",response.getMsg());
                    return resultMap;
                }
                JSONObject pageJson = response.getPage();
                if(pageJson != null){
                    count = pageJson.getInteger("total");
                }
//                JSONArray jsonArray = response.getData();
                data = JSON.parseArray(response.getData(), DeviceInfo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultMap.put("code",0);
        resultMap.put("msg","success");
        resultMap.put("count",count);
        resultMap.put("data",data);
        return resultMap;
    }

    @Override
    public Map<String, Object> listCam(Integer page,Integer limit) {
        Map<String,Object> resultMap = new HashMap<>();
        ResultEntity tokenEntity = getToken();
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            resultMap.put("code",CommonCodeEntity.FAIL);
            resultMap.put("msg",tokenEntity.getResultDesc());
            return resultMap;
        }
        Map<String, String> params =new HashMap<>();
        params.put("accessToken", (String) tokenEntity.getResultContent());
        // 个人版设备接入上限：10台，无分页功能；如需更完善的功能,请升级为企业版
        params.put("pageStart", String.valueOf(page));
        params.put("pageSize", String.valueOf(limit)); //默认为10，最大为50
        int count = 0;
        List<CameInfo> data = new ArrayList<>();
        try {
            HttpClientResult clientResult = HttpClientPoolUtil.doPost(cameraUrl, params, true);
            if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult.getCode())){
                DataResult2 response = JSON.parseObject(clientResult.getContent(), DataResult2.class);
                if(response == null || !"200".equals(response.getCode())){
                    resultMap.put("code",CommonCodeEntity.FAIL);
                    resultMap.put("msg",response.getMsg());
                    return resultMap;
                }
                JSONObject pageJson = response.getPage();
                if(pageJson != null){
                    count = pageJson.getInteger("total");
                }
                data = JSON.parseArray(response.getData(), CameInfo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultMap.put("code",0);
        resultMap.put("msg","success");
        resultMap.put("count",count);
        resultMap.put("data",data);
        return resultMap;
    }

    @Override
    public Map<String, Object> listChannelByDevice(String deviceSerial) {
        Map<String,Object> resultMap = new HashMap<>();
        ResultEntity tokenEntity = getToken();
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            resultMap.put("code",CommonCodeEntity.FAIL);
            resultMap.put("msg",tokenEntity.getResultDesc());
            return resultMap;
        }
        String token = (String) tokenEntity.getResultContent();
        return listChannel(deviceSerial,token);
    }

    private Map<String,Object> listChannel(String deviceSerial,String token){
        Map<String,Object> resultMap = new HashMap<>();
        Map<String, String> params =new HashMap<>();
        params.put("accessToken", token);
        params.put("deviceSerial", deviceSerial);
        List<ChannelInfo> data = new ArrayList<>();
        try {
            HttpClientResult clientResult = HttpClientPoolUtil.doPost(channelUrl, params, true);
            if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult.getCode())){
                DataResult2 response = JSON.parseObject(clientResult.getContent(), DataResult2.class);
                if(response == null || !"200".equals(response.getCode())){
                    resultMap.put("code",CommonCodeEntity.FAIL);
                    resultMap.put("msg",response.getMsg());
                    return resultMap;
                }
                data = JSON.parseArray(response.getData(), ChannelInfo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultMap.put("code",0);
        resultMap.put("msg","success");
        resultMap.put("count",data.size());
        resultMap.put("data",data);
        return resultMap;
    }

    private ResultEntity querySecret(String communityId){
        HashMap param = new HashMap();
        param.put("communityId",communityId);
        CdysylivesSecretkey secretkey = secretkeyMapper.selectOne(param);
        if(secretkey == null){
            return new ResultEntity(CommonCodeEntity.FAIL,"该小区未配置秘钥",null);
        }
        ResultEntity tokenEntity = queryToken(secretkey.getAppKey(), secretkey.getAppSecret());
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            return new ResultEntity(CommonCodeEntity.FAIL,tokenEntity.getResultDesc(),null);
        }
        return tokenEntity;
    }

    @Override
    public Map<String, Object> listChannelByDevice(String deviceSerial, String communityId) {
        Map<String,Object> resultMap = new HashMap<>();
        ResultEntity tokenEntity = querySecret(communityId);
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            resultMap.put("code",CommonCodeEntity.FAIL);
            resultMap.put("msg",tokenEntity.getResultDesc());
            return resultMap;
        }
        return listChannel(deviceSerial, (String) tokenEntity.getResultContent());
    }

    @Override
    public ResultEntity getVideoAddress(String deviceSerial,String quality,String channelNo) {
        if(StringUtils.isEmpty(deviceSerial)){
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"直播源不存在",null);
        }
        if(StringUtils.isEmpty(quality)){
            quality = "2";
        }
        if(StringUtils.isEmpty(channelNo)){
            channelNo = "1";//默认是1
        }
        ResultEntity tokenEntity = getToken();
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            return new ResultEntity(CommonCodeEntity.SUC_CODE,tokenEntity.getResultDesc(),null);
        }
        String token = (String) tokenEntity.getResultContent();
        return queryVideoAddress(token,deviceSerial,quality,channelNo);
    }

    @Override
    public ResultEntity getVideoAddress(String communityId, String deviceSerial,
                                        String quality, String channelNo) {
        ResultEntity tokenEntity = querySecret(communityId);
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            return tokenEntity;
        }
        String token = (String) tokenEntity.getResultContent();
        return queryVideoAddress(token,deviceSerial,quality,channelNo);
    }

    private ResultEntity queryVideoAddress(String token,String deviceSerial,
                                           String quality, String channelNo){
        Map<String,String> params = new HashMap<>();
        params.put("accessToken", token);
        params.put("deviceSerial",deviceSerial);
        params.put("quality",quality); //1高清2流畅
        params.put("channelNo",channelNo);
        try {
            HttpClientResult clientResult = HttpClientPoolUtil.doPost(addressUrl, params, true);
            if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult.getCode())){
                DataResult response = JSON.parseObject(clientResult.getContent(), DataResult.class);
                JSONObject jsonObject = response.getData();
                if(jsonObject != null){
                    String url = jsonObject.getString("url");
                    Map<String,String> resultMap = new HashMap<>();
                    resultMap.put("token", token);
                    resultMap.put("url",url);
                    return new ResultEntity(CommonCodeEntity.SUC_CODE,"获取播放地址成功",resultMap);
                }
            }
            return new ResultEntity(CommonCodeEntity.FAIL,clientResult.getContent(),null);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return new ResultEntity(CommonCodeEntity.FAIL,"获取播放地址失败",null);
    }

    @Override
    public CdysylivesMonitorDevice selectOne(HashMap param) {
        return monitorDeviceMapper.selectOne(param);
    }

    @Override
    public List<CdysylivesMonitorDevice> list(HashMap param) {
        return monitorDeviceMapper.list(param);
    }

    @Override
    public List<CdysylivesMonitorDevice> listPage(HashMap param) {
        return monitorDeviceMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return monitorDeviceMapper.listPageCount(param);
    }

    @Override
    public JsonResult saveOrUpdate(CdysylivesMonitorDevice record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            record.setId(String.valueOf(idWorker.nextId()));
            record.setCreatetTime(LocalDateTime.now());
            record.setIsEnabled(Constant.DISABLED);
            count = monitorDeviceMapper.insert(record);
            msg = "新增成功";
        }else {
            count = monitorDeviceMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("ID不存在");
        }
        int result = monitorDeviceMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult batchEnable(List<String> idList) {
        if(idList.size() == 0){
            return JsonResult.error("参数不存在");
        }
        int num = monitorDeviceMapper.batchEnable(idList);
        return num > 0 ? JsonResult.ok("启用成功",null) : JsonResult.error("启用失败");
    }

    @Override
    public JsonResult batchDisable(List<String> idList) {
        if(idList.size() == 0){
            return JsonResult.error("参数不存在");
        }
        int num = monitorDeviceMapper.batchDisable(idList);
        return num > 0 ? JsonResult.ok("禁用成功",null) : JsonResult.error("禁用失败");
    }

    @Override
    public JsonResult queryBuildingList(String communityId) {
        return cdysyliveRpc.queryBuildingList(communityId);
    }

    @Override
    public JsonResult listDeviceType() {
        List<String> list = monitorDeviceMapper.listDeviceType();
        List<String> typeList =
                list.stream().filter(item -> !StringUtils.isEmpty(item)).collect(Collectors.toList());
        return JsonResult.ok("success",typeList);
    }

    private CdysylivesSecretkey getSecretkey(String communityId){
        HashMap param = new HashMap();
        param.put("communityId",communityId);
        return secretkeyMapper.selectOne(param);
    }

    @Override
    public JsonResult syncDevice(String communityId) {
        CdysylivesSecretkey secretkey = getSecretkey(communityId);
        if(secretkey == null){
            return JsonResult.error("社区秘钥未设置");
        }
        ResultEntity tokenEntity = queryToken(secretkey.getAppKey(),secretkey.getAppSecret());
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            return JsonResult.error(tokenEntity.getResultDesc());
        }
        int size = Constant.DEFAULT_PAGE_SIZE;
        Map<String, String> params =new HashMap<>();
        params.put("accessToken", (String) tokenEntity.getResultContent());
        params.put("pageStart", "0");
        params.put("pageSize", size+""); //默认为10，最大为50
        int count = 0;
        List<DeviceInfo> data = new ArrayList<>();
        try {
            HttpClientResult clientResult = HttpClientPoolUtil.doPost(deviceUrl, params, true);
            if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult.getCode())){
                DataResult2 response = JSON.parseObject(clientResult.getContent(), DataResult2.class);
                if(response == null || !"200".equals(response.getCode())){
                    return JsonResult.error(response.getMsg());
                }
                JSONObject pageJson = response.getPage();
                if(pageJson != null){
                    count = pageJson.getInteger("total");
                }
                data = JSON.parseArray(response.getData(), DeviceInfo.class);
                if(count > size){ //存在下一页
                    // 计算页数
                    int page = count % size == 0 ? count/size : count/size+1;
                    for (int i = 1; i < page; i++) {
                        params.put("pageStart",i+"");
                        HttpClientResult clientResult2 = HttpClientPoolUtil.doPost(deviceUrl, params, true);
                        if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult2.getCode())) {
                            DataResult2 response2 = JSON.parseObject(clientResult2.getContent(), DataResult2.class);
                            if (response2 == null || !"200".equals(response2.getCode())) {
                                continue;
                            }
                            List<DeviceInfo> infos = JSON.parseArray(response2.getData(), DeviceInfo.class);
                            data.addAll(infos);
                        }
                    }
                }
            }else {
                return JsonResult.error("获取监控设备失败:" + clientResult.getContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("total",data.size());
        resultMap.put("list",data);
        return JsonResult.ok("success",resultMap);

    }
    @Override
    public JsonResult batchConfirmDevice(List<DeviceInfo> deviceInfoList) {
        if(deviceInfoList == null || deviceInfoList.size() == 0){
            return JsonResult.error("请选择设备");
        }
        String communityId = deviceInfoList.get(0).getCommunityId();
        CdysylivesSecretkey secretkey = getSecretkey(communityId);
        if(secretkey == null){
            return JsonResult.error("社区秘钥未设置");
        }
        int addCount = 0,updateCount = 0;
        for(DeviceInfo deviceInfo : deviceInfoList){
            String deviceSerial = deviceInfo.getDeviceSerial();
            if(StringUtils.isEmpty(deviceSerial)){continue;}
            HashMap param2 = new HashMap();
            param2.put("deviceSerial",deviceSerial);
            CdysylivesMonitorDevice monitorDevice = monitorDeviceMapper.selectOne(param2);
            int status = deviceInfo.getStatus();
            String deviceName = deviceInfo.getDeviceName();
            if(monitorDevice == null){
                monitorDevice = new CdysylivesMonitorDevice();
                monitorDevice.setId(String.valueOf(idWorker.nextId()));
                monitorDevice.setDeviceName(deviceName);
                monitorDevice.setDeviceType(deviceInfo.getDeviceType());
                monitorDevice.setCommunityId(communityId);
                monitorDevice.setCommunityName(secretkey.getCommunityName());
                monitorDevice.setDeviceSerial(deviceSerial);
                monitorDevice.setCreatetTime(LocalDateTime.now());
                monitorDevice.setIsEnabled(Constant.DISABLED);
                monitorDevice.setDeviceStatus(status);
                monitorDeviceMapper.insert(monitorDevice);
                addCount++;
            }else {
                CdysylivesMonitorDevice newDevice = new CdysylivesMonitorDevice();
                newDevice.setCommunityId(communityId);
                monitorDevice.setCommunityName(secretkey.getCommunityName());
                newDevice.setId(monitorDevice.getId());
                newDevice.setDeviceName(deviceName);
                newDevice.setDeviceStatus(status);
                monitorDeviceMapper.updateByKey(newDevice);
                updateCount++;
            }
        }
        return JsonResult.ok("成功同步"+addCount+"个设备,更新"+updateCount+"个设备.",null);
    }

    @Override
    public JsonResult listTask(String deviceId) {
        HashMap param = new HashMap();
        param.put("deviceId",deviceId);
        List<CdysylivesDeviceTask> list = deviceTaskMapper.list(param);
        return list.size() > 0 ? JsonResult.ok("success",list) : JsonResult.error("无回放文件");
    }

    @Override
    public Map<String, Object> listFile(String taskId,String communityId) {
        Map<String,Object> resultMap = new HashMap<>();
        if(StringUtils.isEmpty(taskId)){
            resultMap.put("code",CommonCodeEntity.FAIL);
            resultMap.put("msg","无数据");
            return resultMap;
        }
        ResultEntity tokenEntity = querySecret(communityId);
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            resultMap.put("code",CommonCodeEntity.FAIL);
            resultMap.put("msg",tokenEntity.getResultDesc());
            return resultMap;
        }
        String token = (String) tokenEntity.getResultContent();
        Map<String,String> params = new HashMap<>();
        params.put("accessToken",token);
        params.put("taskId",taskId);
        //params.put("pageNumber",Constant.STRING_ZERO);//分页页码，以0开始，默认为0
        params.put("pageSize",Constant.DEFAULT_PAGE_SIZE+"");//分页大小，从1开始，不能大于50
        //params.put("sortField","createTime");//排序字段，默认为createTime，表示创建时间
        params.put("sortRule","desc");//排序规则，asc：升序；desc：降序
        //是否需要下载链接， true:需要，false不需要，默认值是false。
        // 若任务中的文件移动至归档存储项目中，且没有解冻，该文件的下载链接会为空
        params.put("hasUrl","true");
        List<FileInfo> data = new ArrayList<>();
        try {
            HttpClientResult clientResult = HttpClientPoolUtil.doGet(this.fileListUrl, params, true);
            if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult.getCode())){
                YsyResult response = JSON.parseObject(clientResult.getContent(), YsyResult.class);
                MetaInfo meta = response.getMeta();
                if(!Constant.REQUEST_STATUS_SUCCEED.equals(meta.getCode())){
                    resultMap.put("code",meta.getCode());
                    resultMap.put("msg",meta.getMessage());
                    return resultMap;
                }
                data = JSON.parseArray(response.getData(), FileInfo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultMap.put("code",0);
        resultMap.put("msg","success");
        resultMap.put("count",data.size());
        resultMap.put("data",data);
        return resultMap;
    }

    @Override
    public JsonResult deleteFile(String communityId,String fileId, String projectId) {
        ResultEntity tokenEntity = querySecret(communityId);
        if(CommonCodeEntity.FAIL.equals(tokenEntity.getResultCode())){
            return JsonResult.error(tokenEntity.getResultDesc());
        }
        String token = (String) tokenEntity.getResultContent();
        Map<String,String> params = new HashMap<>();
        params.put("accessToken",token);
        params.put("fileId",fileId);
        params.put("projectId",projectId);
        params.put("newProjectId",projectId);
        try {
            HttpClientResult clientResult = HttpClientPoolUtil.doDelete(this.delFileUrl, params, true);
            if(Constant.REQUEST_STATUS_SUCCEED.equals(clientResult.getCode())){
                YsyResult response = JSON.parseObject(clientResult.getContent(), YsyResult.class);
                MetaInfo meta = response.getMeta();
                if(!Constant.REQUEST_STATUS_SUCCEED.equals(meta.getCode())){
                    return JsonResult.error(meta.getMessage());
                }
                return JsonResult.ok(meta.getMessage(),null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("删除文件异常: " + e.getMessage());
        }
        return JsonResult.error("删除失败,请重试或联系管理员");
    }

    @Override
    public String queryCommunityIdByUId(String userId) {
        return monitorDeviceMapper.queryCommunityIdByUId(userId);
    }
}
